#include "soft_keyboard.h"

soft_Keyboard::soft_Keyboard(QWidget *parent) : QMainWindow(parent)
{
    this->setStyleSheet("QMainWindow{  border:3px solid rgb(255, 188, 88);background-color: rgb(11, 180, 200);\
                        border-radius: 3px;		                 /* 圆角设置 */\
                                    /* 背景颜色 */}");
//    this->setFixedSize(380,480);
    this->setGeometry(60,20,500,200);//

    ui_init();
    connect_init();
}

    void soft_Keyboard::ui_init()
    {
        QFont font;
         //设置文字大小
         font.setPointSize(16);
        for(int i=0;i<16;i++)//创建主屏幕显示的按键
        {
             theBtn[i] = new QPushButton(this);
             theBtn[i]->setStyleSheet("QPushButton{\
                                    outline: 0px ;                            /* 去掉获取焦点时的虚线 */ \
                                    background-color: rgb(255, 255, 222);	    /* 背景颜色 */\
                                    border-radius: 6px;		                 /* 按钮边框的圆角设置 */\
                                    border-style: outset;\
                                    border: 1px groove gray;\
                                    padding:1px 4px;\
                                    /* 按钮文本设置 */\
                                     text-align: center;			             /* 文本的位置 */\
                                    font-size: 20px;\
                                    color: #000000;			                 /* 文本颜色 */\
                               }"
        "QPushButton:focus{\
        outline: 0px ;                            /* 去掉获取焦点时的虚线 */ \
        background-color: rgb(222,20,20);	    /* 背景颜色 */\
        border-radius: 6px;		                 /* 按钮边框的圆角设置 */\
        border-style: outset;\
           /* 按钮文本设置 */\
        text-align: center;			             /* 文本的位置 */\
        font-size: 22px;\
        qproperty-iconSize: 20px 20px;			/* 设置图标大小*/\
        color: rgb(12, 125, 125);			                 /* 文本颜色 */\
        }"
        "QPushButton:pressed{background-color:rgb(99,211,133);}"
    //    "QPushButton:hover{background-color:rgb(255,127,39);}"
    "QPushButton:checked{background-color:rgb(12,127,39);}"
    "QPushButton:clicked{background-color:rgb(35,23,39);}"
    //"QPushButton:pressed{background-color:rgb(255,23,39);}"
    //"QPushButton:released{background-color:rgb(123,23,39);}"

    //    "QPushButton:KeyRelease{background-color:rgb(222,66,99);}"

        );
        BtnList.append(theBtn[i]);
        switch (i)
        {
        case 0:
        {
            theBtn[0]->setGeometry(10,80,60,60);//0按键
            theBtn[0]->setText(("0"));
            break;
        }
        case 1:
        {
            theBtn[1]->setGeometry(85,80,60,60);//1按键
            theBtn[1]->setText(("1"));
            break;
        }
        case 2:
        {
            theBtn[2]->setGeometry(160,80,60,60);//2按键
            theBtn[2]->setText(("2"));
            break;
        }
        case 3:
        {
            theBtn[3]->setGeometry(235,80,60,60);//3按键
            theBtn[3]->setText(("3"));
            break;
        }
        case 4:
        {
            theBtn[4]->setGeometry(10,80,60,60);//4按键
            theBtn[4]->setText(("4"));

            break;
        }
        case 5:
        {
            theBtn[5]->setGeometry(85,80,60,60);//5按键
            theBtn[5]->setText(("5"));

            break;
        }
        case 6:
        {
            theBtn[6]->setGeometry(160,80,60,60);//6按键
            theBtn[6]->setText(("6"));
            break;
        }
        case 7:
        {
            theBtn[7]->setGeometry(235,80,60,60);//7按键
            theBtn[7]->setText(("7"));
            break;
        }
        case 8:
        {
            theBtn[8]->setGeometry(10,80,60,60);//8按键
            theBtn[8]->setText(("8"));
            break;
        }
        case 9:
        {
            theBtn[9]->setGeometry(85,80,60,60);//9按键
            theBtn[9]->setText(("9"));
            break;
        }
        case 10:
        {
            theBtn[10]->setGeometry(160,100,50,25);//-按键
            theBtn[10]->setText(tr("-"));
            break;
        }
        case 11:
        {
            theBtn[11]->setGeometry(210,100,50,25);//.按键
            theBtn[11]->setText(tr("."));
            break;
        }
        case 12:
        {
            theBtn[12]->setGeometry(260,100,50,25);//删除按键
            theBtn[12]->setText(tr("×"));
            break;
        }
        case 13:
        {
            theBtn[13]->setGeometry(10,10,150,25);//显示按键
            break;
        }
        case 14:
        {
            theBtn[14]->setGeometry(160,10,60,60);//OK按键
            theBtn[14]->setText(tr("OK"));
            break;
        }
        case 15:
        {
            theBtn[15]->setGeometry(235,10,60,60);//CanCel按键
            theBtn[15]->setText(tr("CanCel"));
            break;
        }
        }

        }
          for(int i=0;i<BtnList.length();i++)
        {
            BtnList.at(i)->installEventFilter(this);
            if(i<BtnList.length()-1)
                setTabOrder(BtnList.at(i),BtnList.at(i+1));
        }
        theBtn[14]->setFocus();
    }
    void soft_Keyboard::recive_otherdata(QString num)
    {
        theBtn[13]->setText(num);
    }

    void soft_Keyboard::connect_init()
    {
        connect(theBtn[0],&QPushButton::clicked,[=](){
            theBtn[13]->setText(theBtn[13]->text()+"0");
        });
        connect(theBtn[1],&QPushButton::clicked,[=](){
            theBtn[13]->setText(theBtn[13]->text()+"1");
        });
         connect(theBtn[2],&QPushButton::clicked,[=](){
            theBtn[13]->setText(theBtn[13]->text()+"2");
        });
        connect(theBtn[3],&QPushButton::clicked,[=](){
            theBtn[13]->setText(theBtn[13]->text()+"3");
        });
        connect(theBtn[4],&QPushButton::clicked,[=](){
            theBtn[13]->setText(theBtn[13]->text()+"4");
        });
        connect(theBtn[5],&QPushButton::clicked,[=](){
            theBtn[13]->setText(theBtn[13]->text()+"5");
        });
        connect(theBtn[6],&QPushButton::clicked,[=](){
            theBtn[13]->setText(theBtn[13]->text()+"6");
        });
        connect(theBtn[7],&QPushButton::clicked,[=](){
            theBtn[13]->setText(theBtn[13]->text()+"7");
        });
        connect(theBtn[8],&QPushButton::clicked,[=](){
            theBtn[13]->setText(theBtn[13]->text()+"8");
        });
        connect(theBtn[9],&QPushButton::clicked,[=](){
            theBtn[13]->setText(theBtn[13]->text()+"9");
        });
        connect(theBtn[10],&QPushButton::clicked,[=](){
            theBtn[13]->setText(theBtn[13]->text()+"-");
        });
        connect(theBtn[11],&QPushButton::clicked,[=](){
            theBtn[13]->setText(theBtn[13]->text()+".");
        });
        connect(theBtn[12],&QPushButton::clicked,[=](){
                    QString text = theBtn[13]->text();
                     theBtn[13]->setText(text.remove(text.length()-1,1));
        });
            connect(theBtn[14] ,&QPushButton::clicked,[=](){
                if(theBtn[13]->text().length()>0)
                {
                    NumKeyBoard= theBtn[13]->text();
                    emit changename2(NumKeyBoard);
                }
                else
                    NumKeyBoard = "NULL";
                this->close();
            });
                connect(theBtn[15] ,&QPushButton::clicked,[=](){
                    NumKeyBoard = "NULL";
                    this->close();
                });
    //    connect(theBtndel ,&QPushButton::clicked,[=](){
    //        QString text = theBtn[13]->text();
    //         theBtn[13]->setText(text.remove(text.length()-1,1));
    //    });
    //    connect(theBtnok ,&QPushButton::clicked,[=](){
    //        if(theBtn[13]->text().length()>0)
    //        {
    //            NumKeyBoard= theBtn[13]->text();
    //            emit changename2(NumKeyBoard);
    //        }
    //        else
    //            NumKeyBoard = "NULL";
    //        this->deleteLater();
    //    });

    }
    bool soft_Keyboard::eventFilter(QObject *watched, QEvent *event)
    {
        for(int i=0;i<BtnList.length();i++)
            if(watched==BtnList.at(i))
        {
            if(event->type() == QEvent::KeyPress)
            {
               QKeyEvent *ev = (QKeyEvent *)event;
               QPushButton *button = (QPushButton *)watched;
               switch (ev->key())
               {
               case UP:
                 {
                   if(watched==theBtn[14] ||watched==theBtn[15])
                       break;
                   else if(watched ==theBtn[0] || watched == theBtn[1]|| watched == theBtn[2])
                   {
                       theBtn[14]->setFocus();
                       break;
                   }
                   else if(watched==theBtn[3])
                   {
                       theBtn[15]->setFocus();
                       break;
                   }
                   else if(watched==theBtn[12])
                   {
                       theBtn[7]->setFocus();
                       break;
                   }
                   else
                   {
                       focusPreviousChild();focusPreviousChild();focusPreviousChild();focusPreviousChild();
                       //qDebug()<<focusWidget()->objectName();
                       break;
                   }
                 }
               case OK:
                 {
                       if(watched!= theBtn[14])
                       {
                           emit button->clicked();
                       }
                       else
                       {
                           if(theBtn[13]->text().length()>0)
                           {
                               NumKeyBoard= theBtn[13]->text();
                               emit changename2(NumKeyBoard);
                           }
                           this->deleteLater();
                       }
                       break;
                 }
               case DOWN:
                 {
                   if(watched ==theBtn[8] || watched ==theBtn[9] ||watched ==theBtn[10] ||watched ==theBtn[11]||watched ==theBtn[12] )
                       break;
                   else
                   {
                        focusNextChild();focusNextChild();focusNextChild();focusNextChild();
                        break;
                   }
                   break;
                 }
               case LEFT:
                 {
                   if(watched !=theBtn[14])
                       focusPreviousChild();
                       break;
                 }
               case RIGHT:
                 {
                   if(watched !=theBtn[12])
                       focusNextChild();
                       break;
                 }
               case BACK:
               {
                   this->parentWidget()->show();
                   this->close();
                   break;
               }
               case INIT:
               {
                   init_Show *show = new init_Show(this);
                   show->show();
                   this->hide();

               }
                default:
                   break;
               }
            }
    //        else if (event->type()==QEvent::FocusIn)     //然后再判断控件的具体事件 (这里指获得焦点事件)
    //                  {
    //                      QPushButton *focus_button = (QPushButton *)watched;
    //                      focus_button->setStyleSheet("QPushButton{color:black;background-color: rgb(255, 255, 255);\
    //                                                  border-radius:4px;  border: 2px groove blue; padding:1px 4px;\
    //                                                 border-style: outset;}");
    //                  }
    //        else if (event->type()==QEvent::FocusOut)    // 这里指 lineEdit1 控件的失去焦点事件
    //                 {
    //                    QPushButton *focus_button = (QPushButton *)watched;
    //                    focus_button->setStyleSheet("QPushButton{color:black;background-color: rgb(255, 255, 255);\
    //                                                border-radius:4px;  border: 1px groove gray; padding:1px 4px;\
    //                                               border-style: outset;}");
    //                 }

    }
    }
